package com.samtech.web.view;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.Ordered;
import org.springframework.util.ResourceUtils;
import org.springframework.util.StreamUtils;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.resource.ResourceUrlProvider;
import org.springframework.web.servlet.view.AbstractUrlBasedView;
import org.springframework.web.util.WebUtils;


public class HtmlCacheViewResolver implements ViewResolver, Ordered{
	private static Logger logger=LoggerFactory.getLogger(HtmlCacheViewResolver.class);
	private String checkSuffix="html";
	@Autowired
	ResourceUrlProvider resourceUrlProvider;
	@Autowired
	HttpServletRequest httpServletRequest;
	//@Autowired
	//PathResourceResolver pathResourceResolver;
	private List<String> prefixs=new ArrayList<String>();
	
	
	public String getCheckSuffix() {
		return checkSuffix;
	}


	public void setCheckSuffix(String checkSuffix) {
		this.checkSuffix = checkSuffix;
	}


	public List<String> getPrefixs() {
		return prefixs;
	}


	public void setPrefixs(List<String> prefixs) {
		this.prefixs = prefixs;
	}

	
	@Override
	public View resolveViewName(String viewName, Locale locale) throws Exception {
		if(checkSuffix!=null && viewName.endsWith(checkSuffix)) {
			//ResourceResolverChain chain=null;
			//pathResourceResolver.resolveUrlPath(viewName, CollectionUtils.arrayToList(pathResourceResolver.getAllowedLocations()), chain);
			/*ResourceResolverChain chain = new DefaultResourceResolverChain(handler.getResourceResolvers());
			String resolved = chain.resolveUrlPath(viewName, handler.getLocations());*/
			logger.warn("look view:"+viewName);
			System.out.println("ll view:"+viewName);
			ServletContext servletContext = httpServletRequest.getServletContext();
			String lookpath = WebUtils.getRealPath(servletContext, viewName);
			if(lookpath!=null) {
				logger.warn("found view:"+lookpath);
				System.out.println("ll path:"+viewName);
				AbstractUrlBasedView view= new AbstractUrlBasedView() {
					
					@Override
					protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request,
							HttpServletResponse response) throws Exception {
						response.setContentType(this.getContentType());
						
						ServletContext servletContext = httpServletRequest.getServletContext();
						String lookpath = WebUtils.getRealPath(servletContext, viewName);
						File file = ResourceUtils.getFile(lookpath);
						FileInputStream stream = new FileInputStream(file);
						ServletOutputStream out = response.getOutputStream();
						try {
						if(stream!=null) {
							StreamUtils.copy(stream, out);
						}
						}catch (Exception e) {
							if(stream!=null) {
								stream.close();
							}
						}
					}
				};
				
				view.setContentType("text/html;charset=utf-8");
				return view;
			}
		}else {
			logger.warn("res view name:"+viewName);
			System.out.println("not view path:"+viewName);
		}
		return null;
	}
	public void initialize(ConfigurableApplicationContext applicationContext) {
	
	};

	@Override
	public int getOrder() {
	
		return Integer.MAX_VALUE-5;
	}


	


	
}
